//
//  HZDayNightTableCell.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZDayNightTableCell.h"

@interface HZDayNightTableCell ()

@end

@implementation HZDayNightTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_titleLable];
        
        self.sw = [[UISwitch alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_sw];
        
        
        /**
         *  NSUserDefaults是一个单例，在整个程序中只有一个实例对象，他可以用于数据的永久保存，而且简单实用，这是它可以让数据自由传递的一个前提
         */
        NSString *state = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
        if ([state isEqualToString:@"yes"]) {
            _sw.on = YES;
        }else {
            _sw.on = NO;
        }

        // 夜间模式按钮 事件
        [self.sw addTarget:self action:@selector(swAction:) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _titleLable.frame = CGRectMake(15, 5, self.bounds.size.width / 3, 30);
    
    _sw.frame = CGRectMake(self.bounds.size.width - 60, 7, 45, 20);
    
}

// 这是夜间模式事件
- (void)swAction:(UISwitch *)sw {
//    打算通过通知传递到VC 根据sw的开关进行修改
    
    if (sw.on == YES) {
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
        
        NSNotificationCenter *centerTWO = [NSNotificationCenter defaultCenter];
        
        [centerTWO postNotificationName:@"heibai" object:[UIColor colorWithRed:49 / 255.0 green:47 / 255.0 blue:53 / 255.0 alpha:1]];
        
        NSNotificationCenter *centerLable = [NSNotificationCenter defaultCenter];
        [centerLable postNotificationName:@"heibai" object:[UIColor clearColor]];
        
        NSLog(@"这是YES");
        
    }else {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
        NSNotificationCenter *centerTWO = [NSNotificationCenter defaultCenter];
        
        [centerTWO postNotificationName:@"heibai" object:[UIColor clearColor]];
        
        NSNotificationCenter *centerLable = [NSNotificationCenter defaultCenter];
        [centerLable postNotificationName:@"heibai" object:[UIColor whiteColor]];
        
        NSLog(@"这是NO");
    }

    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
